//
//  MyTopView.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/19.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "MyTopView.h"

@interface MyTopView()

@property (nonatomic, strong) UIButton *userInfoButton;
@property (nonatomic, strong) UIImageView *rightImageView;

@end

@implementation MyTopView

- (void)initSubViews
{
    self.backgroundColor = UIColorFromRGB(0xFE8562);
    
    _userInfoButton = [[UIButton alloc]init];
    [_userInfoButton addTarget:self action:@selector(userInfoAcion:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:_userInfoButton];
    
    _rightImageView = [[UIImageView alloc]init];
    _rightImageView.image = [UIImage imageNamed:@"wode_qianjin"];
    [self addSubview:_rightImageView];
    
    _userImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, HFTopStatusHeight+20, 60, 60)];
    _userImageView.contentMode = UIViewContentModeScaleAspectFill;
    _userImageView.backgroundColor = UIColorFromRGB(0xffffff);
    _userImageView.layer.cornerRadius = 30;
    _userImageView.image = [UIImage imageNamed:@"mine_normalhead"];
    
    if ([[CBTImageTools getUserImageNameWithUserID:[CBTLocalUserManager sharedInstance].userId] isEqualToString:@"-1"]) {
         [_userImageView lf_setImageURL:[NSString stringWithFormat:@"%@/%@",kBaseURL_PICSTATIC,[CBTLocalUserManager sharedInstance].headImage]];
    }
    else {
      _userImageView.image = [UIImage imageWithContentsOfFile:[CBTImageTools getUserImageNameWithUserID:[CBTLocalUserManager sharedInstance].userId]];
    }
    
    _userImageView.layer.masksToBounds = YES;
    [self addSubview:_userImageView];
 
    _userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(_userImageView.right + 15, HFTopStatusHeight+20, 90, 18)];
    _userNameLabel.text = [CBTLocalUserManager sharedInstance].nickName;
    _userNameLabel.font = [UIFont systemFontOfSize:16];
    _userNameLabel.textColor = [UIColor whiteColor];
    [self addSubview:_userNameLabel];
    
    _userIDLabel = [[UILabel alloc]initWithFrame:CGRectMake(_userImageView.right + 15, _userNameLabel.bottom + 15, ScreenNarrowSide - (_userImageView.right + 15) - 38, 14)];
    NSString *avatar    = [CBTLocalUserManager sharedInstance].avatar;
    NSString *avatarStr = [NSString stringWithFormat:@"%@：%@",kLocalizedString(@"邀请码", nil),[CBTLocalUserManager sharedInstance].avatar];
    _userIDLabel.font = [UIFont systemFontOfSize:14];
   
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:avatarStr];
    [string setAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.],NSForegroundColorAttributeName: [UIColor whiteColor]} range:NSMakeRange(0, avatarStr.length - avatar.length + 1)];
    [string setAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.],NSForegroundColorAttributeName: DefaultBlueColor} range:NSMakeRange(avatarStr.length - avatar.length, avatar.length)];
    _userIDLabel.attributedText = string;
    [self addSubview:_userIDLabel];
    
    _userIDLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cpyAatar)];
    [_userIDLabel addGestureRecognizer:tap];
    
    _signInView = [[UIView alloc]init];
    [self addSubview:_signInView];
    
    _signInImageView       = [[UIImageView alloc]init];
    _signInImageView.image = [UIImage imageNamed:@"vip0"];
    [_signInView addSubview:_signInImageView];
    [_signInImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(20);
    }];
    
}

- (void)cpyAatar {
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = [CBTLocalUserManager sharedInstance].avatar;
    [HFStatusView showSuccessMessage:kLocalizedString(@"已复制到剪贴板",nil)];
}

-(void)refreshSignInStatus:(NSString *)status
{
    [_userNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self->_userImageView.right + 15);
        make.top.mas_equalTo(20+HFTopStatusHeight);
        make.height.mas_equalTo(18);
    }];
    
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    @weak(self)
    [_rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(40 + HFTopStatusHeight);
        make.width.mas_equalTo(8);
        make.height.mas_equalTo(15);
    }];
    
    [_signInView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.userNameLabel.mas_right).offset(10);
        make.top.mas_equalTo(self_weak_.userNameLabel.mas_top);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(20);
    }];

    [_userInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_equalTo(44);
        make.height.mas_equalTo(60);
    }];
}

- (void)userInfoAcion:(UIButton *)btn
{
    if (self.pushUserInfoBlock != nil) {
        self.pushUserInfoBlock(0);
    }
}

- (void)withdrawAction:(UIButton *)btn
{
    if (self.pushUserInfoBlock != nil) {
        self.pushUserInfoBlock(1);
    }
}


@end
